<?php
/**
 * Created by PhpStorm.
 * User: lixy
 * Date: 2016/1/19
 * Time: 13:16
 */

namespace app\controllers;

use Yii;
use app\lib\WXBaseController;
use app\lib\WebService;;
use app\models\HomeDao;
use app\models\FxDao;
use app\lib\WeixinUtils;
use app\lib\ImageTools;
use app\models\weixin\T_WXMALL_USER;
class WxGameController extends WXBaseController
{

    private static function str_change($text){
        if(mb_strlen($text)==2){
            $tmp=mb_substr($text,0,1,'utf-8').' '.mb_substr($text,1,1,'utf-8');
            $text=$tmp;
        }
        return $text;
    }
    public function actionGame1Share($text=''){
        return $this->render('game1-share',['text'=>$text]);
    }
    public function actionGame1($text=''){
        return $this->render('game1',['text'=>$text]);
    }
    //mahuateng  game1-pic
    public function actionGame1Pic($text=''){
        $text=static::str_change($text);
        $path_bg='./game/mahuateng.jpg';
//        $text='马云';
        $path_font='./fonts/simhei.ttf';
        $font_size=13;
        $font_top=145;
        $font_left=450;
        $font_angle=5;
        $bg_img = imagecreatefromjpeg($path_bg);

        // 文字
        $textcolor = imagecolorallocate($bg_img, 255, 255, 255); //设置一个颜色变量为黑色

        $zi_chang=ImageTools::text_width($text,$font_size,$path_font) / 2;

        imagettftext($bg_img,$font_size,$font_angle,$font_left-$zi_chang / 2,$font_top,$textcolor,$path_font,$text);


        ob_clean();
        header("Content-type: image/jpeg");

        imagejpeg($bg_img);
        exit;
    }
    public function actionGame1PicFull($text=''){
        $text=static::str_change($text);
        $path_bg='./game/mahuateng_full.jpg';
//        $text='马云';
        $path_font='./fonts/simhei.ttf';
        $font_size=13;
        $font_top=155;
        $font_left=480;
        $font_angle=5;
        $bg_img = imagecreatefromjpeg($path_bg);

        // 文字
        $textcolor = imagecolorallocate($bg_img, 255, 255, 255); //设置一个颜色变量为黑色

        $zi_chang=ImageTools::text_width($text,$font_size,$path_font) / 2;

        imagettftext($bg_img,$font_size,$font_angle,$font_left-$zi_chang / 2,$font_top,$textcolor,$path_font,$text);


        ob_clean();
        header("Content-type: image/jpeg");

        imagejpeg($bg_img);
        exit;
    }
    //分享图   my-share-pic
    public function actionMySharePic($share_id){
       $re=FxDao::get_T_WX_FX_USER_INVITE_CODE($share_id);
        if(count($re)==0){
            echo "error share id!";
            exit;
        }else{
            $NICK_NAME=$re[0]['NICK_NAME'];
            $HEADER_URL=$re[0]['HEADER_URL'];
            $QR_TICKET=$re[0]['QR_TICKET'];
            $days=intval($re[0]['DAYS']);
            if($days>20){
                //更新ticket
                $QR_TICKET= FxDao::update_INVITE_CODE_QR($share_id);
            }
        }

           $imgs = array(
               'header' => $HEADER_URL,
               'qr' => "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={$QR_TICKET}",
               'bg' => './js/fx/fx_share_bg.jpg',
               'text' => $NICK_NAME,
               'font' => './js/fx/msyh.ttc',
           );
         ob_clean();
        header("Content-type: image/jpeg");

        imagejpeg( Fx_Image::mergerImg($imgs));
        exit;
    }
}